<?php
// +----------------------------------------------------------------------
// | Author: guanxu <510104561@qq.com>
// +----------------------------------------------------------------------
namespace App\Http\Controllers\Api;

use App\Models\UserIntegral;
use App\Utils\CodeUtil;
use App\Utils\ConstantUtil;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;

class UserController extends Controller
{
    /**
     * 显示资源列表
     */
    function index(Request $request)
    {
        $select['id'] = $request->input("id");
        $page = $request->input("page", 1);
        $list = User::where("type", ConstantUtil::USER_NORMAL)->orderBy("id", "desc");
        if (!empty($select['id'])) $list = $list->where("id", $select['id']);
        $num = ConstantUtil::PAGE_SHOW;
        $list = $list->paginate($num);
        $count = $list->total();
        foreach ($list as &$lists) {
            $lists["xh"] = $count - ($page - 1) * $num;
            $count--;
        }
        return view('Api/user/index', ["list" => $list, "select" => $select]);
    }

    /**
     * 显示创建资源表单页.
     */
    function create()
    {
        return view('Api/user/create');
    }

    /**
     * 保存新建的资源
     */
    function save(Request $request)
    {
        $input = $request->input();
        $obj = new User();
        foreach($input as $key => $val) {
            if($key == 'name') $obj->email = $val;
            $obj->$key = $val;
        }
        $result = $obj->save();
        if ($result) {
            return response()->json(['code' => CodeUtil::SUCCESS, "msg" => "操作成功!"]);
        } else {
            return response()->json(['code' => CodeUtil::API_ERROR, "msg" => "操作失败!"]);
        }
    }

    /**
     * 显示指定的资源
     */
    function read(Request $request)
    {
        $list = User::where("id", $request->input("id"))->first();
        if (empty($list)) return response()->json(['code' => CodeUtil::API_ERROR, "msg" => "数据不存在!"]);
        return view('Api/user/read', ["list" => $list]);
    }

    /**
     * 显示编辑资源表单页.
     */
    function edit(Request $request)
    {
        $id = $request->input("id");
        $list = User::find($id);
        if (empty($list)) return response()->view("errors::数据不存在");
        return view('Api/user/edit', [
            'list' => $list,
        ]);
    }

    /**
     * 保存更新的资源
     */
    function update(Request $request)
    {
        $id = $request->input("id");
        $data = $request->input();
        unset($data["id"]);
        User::where("id", $id)->update($data);
        return response()->json(['code' => CodeUtil::SUCCESS, "msg" => "操作成功!"]);
    }

    /**
     * 删除指定资源
     */
    function delete(Request $request)
    {
        $id = $request->input("id");
        if (!is_array($id)) $id = [$id];
        if (empty($id)) return response()->view("errors::数据不存在");
        User::whereIn("id", $id)->delete();
        return response()->json(['code' => CodeUtil::SUCCESS, "msg" => "删除成功!"]);
    }
}
